<template>
  <view class="sub-category-menu">
    <view class="menu-title" v-if="title">{{ title }}</view>
    <view class="category-grid">
      <view 
        class="category-item" 
        v-for="item in displayCategories" 
        :key="item.id"
        @click="selectCategory(item)"
      >
        <image :src="item.icon" mode="aspectFit" class="category-icon"></image>
        <text class="category-name">{{ item.name }}</text>
        <text class="category-count" v-if="item.productCount">({{ item.productCount }})</text>
      </view>
    </view>
    
    <view class="expand-btn" v-if="subcategories.length > 3" @click="toggleExpand">
      <text>{{ isExpanded ? '收起' : '更多' }}</text>
      <uni-icons :type="isExpanded ? 'top' : 'bottom'" size="12" color="#666"></uni-icons>
    </view>
  </view>
</template>

<script>
export default {
  name: 'SubCategoryMenu',
  props: {
    title: {
      type: String,
      default: ''
    },
    subcategories: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      isExpanded: false
    }
  },
  computed: {
    displayCategories() {
      return this.isExpanded ? this.subcategories : this.subcategories.slice(0, 3)
    }
  },
  methods: {
    selectCategory(category) {
      this.$emit('select', category)
    },
    toggleExpand() {
      this.isExpanded = !this.isExpanded
    }
  }
}
</script>

<style lang="scss" scoped>
.sub-category-menu {
  padding: 20rpx;
  
  .menu-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 20rpx;
  }
  
  .category-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20rpx;
    
    .category-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 20rpx;
      background-color: #f8f8f8;
      border-radius: 12rpx;
      
      .category-icon {
        width: 80rpx;
        height: 80rpx;
        margin-bottom: 10rpx;
      }
      
      .category-name {
        font-size: 26rpx;
        color: #333;
        text-align: center;
      }
      
      .category-count {
        font-size: 22rpx;
        color: #999;
        margin-top: 4rpx;
      }
    }
  }
  
  .expand-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20rpx 0;
    margin-top: 10rpx;
    
    text {
      font-size: 26rpx;
      color: #666;
      margin-right: 4rpx;
    }
    
    &:active {
      opacity: 0.7;
    }
  }
}
</style> 